package com.android.phoenix.bluetooth;

public class BicycleData {
	final static byte FLAG = 0x7E;
	final static byte ALARM0 = 0x0;
	final static byte ALARM1 = 0x1;
	final static byte ALARM2 = 0x2;
	final static byte ALARM3 = 0x3;
	final static byte ALARM4 = 0x4;
	final static byte ALARM5 = 0x5;
	final static byte ALARM6 = 0x6;
	final static byte ALARM7 = 0x7;
	final static byte ALARM8 = 0x8;
	final static byte ALARM9 = 0x9;
	final static byte HR_DATA = 0xA;
	final static byte HR_MEASURESTATE = 0xB;
	public int intPacketLength = 0;
	public byte btParaLength = 0;
	public byte btCommandId = 0;
	public byte MeasureState = 0;
	public int HeartRate = 0;
	public boolean isAlarm;
	public boolean isHRMeasureStart;
	public boolean isHRMeasureStop;
	private byte[] htData;

	public BicycleData(byte[] data) {
		this.htData = data;
		CRC();
		Parser();
	}
    public void RefreshData(byte[] data)
    {
    	this.htData = data;
		CRC();
		Parser();
    }
	private boolean CRC() {
		if (null == htData) {
			return false;
		}
		byte sum = 0;
		int length;
		length = htData[1] << 8 + htData[2] - 2;
		for (int i = 1; i < length; i++) {
			sum += htData[i];
		}
		if (0 == sum) {
			return true;
		} else {
			return false;
		}
	}

	private void Parser() {
		if (CRC()) {
			if (FLAG == htData[0]) {
				intPacketLength = htData[1] << 8 + htData[2];
				btCommandId = htData[3];
				btParaLength = htData[4];
				switch (btCommandId) {
				case 0: {
					if (1 == btParaLength && htData[5] > 0) {
						isAlarm = true;
					} else {
						isAlarm = false;
					}
					break;
				}
				case 1: {
					if (1 == btParaLength && htData[5] > 0) {
						isAlarm = true;
					} else {
						isAlarm = false;
					}
					break;
				}
				case 2: {
					if (1 == btParaLength && htData[5] > 0) {
						isAlarm = true;
					} else {
						isAlarm = false;
					}
					break;
				}
				case 3: {
					if (1 == btParaLength && htData[5] > 0) {
						isAlarm = true;
					} else {
						isAlarm = false;
					}
					break;
				}
				case 4: {
					if (1 == btParaLength && htData[5] > 0) {
						isAlarm = true;
					} else {
						isAlarm = false;
					}
					break;
				}
				case 5: {
					if (1 == btParaLength && htData[5] > 0) {
						isAlarm = true;
					} else {
						isAlarm = false;
					}
					break;
				}
				case 6: {
					if (1 == btParaLength && htData[5] > 0) {
						isAlarm = true;
					} else {
						isAlarm = false;
					}
					break;
				}
				case 7: {
					if (1 == btParaLength && htData[5] > 0) {
						isAlarm = true;
					} else {
						isAlarm = false;
					}
					break;
				}
				case 8: {
					if (1 == btParaLength && htData[5] > 0) {
						isAlarm = true;
					} else {
						isAlarm = false;
					}
					break;
				}
				case 9: {
					if (1 == btParaLength && htData[5] > 0) {
						isAlarm = true;
					} else {
						isAlarm = false;
					}
					break;
				}
				case 11: {
					if (1 == btParaLength && htData[5] > 0) {
						isHRMeasureStart = true;
					} else {
						isHRMeasureStop = true;
					}
					break;
				}
				}
			}
		}
	}
	
	public byte[] ACK()
	{
		htData[3] |=0x80;
		return htData;
	}
	public int getHeartRate()
	{
		if(0!=HeartRate)
		{
			return HeartRate;
		}
		else
		{
			return -1;
		}
			
	}
}
